/**
 * Project Name:umale-maintenance
 * File Name:Service
 * Package Name:test.spring.data
 * Date: 2018/11/20 14:28
 * Copyright (c) 2018, CCI All Rights Reserved.
 */
package test.spring.data;

import my.spring.ioc.annotation.Bean;
import my.spring.ioc.annotation.Inject;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import test.spring.data.dao.Dao;
import test.spring.ioc.NewsBean;

/**
 * @author fengbo
 * @version 1.0.0
 * @date 2018/11/20 14:28
 * @since JDK 1.8
 */
@Bean("service")
public class Service {

    @Inject("dao")
    private Dao dao;
    private PlatformTransactionManager transactionManager;
    private NewsBean newsBean;

    public void testData() {
        TransactionDefinition definition = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(definition);
        try {
            dao.insert(newsBean);
            Integer.valueOf("ss");
            newsBean.setName("test");
            dao.insert(newsBean);
            transactionManager.commit(status);
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
        }
    }

    private my.spring.transaction.PlatformTransactionManager platformTransactionManager;

    public void testForMyData() {
        my.spring.transaction.status.TransactionStatus status =
                platformTransactionManager.getTransaction(
                        new my.spring.transaction.definition.DefaultTransactionDefinition());

        try {
            dao.insert(newsBean);
            Integer.valueOf("ss");
            newsBean.setName("test");
            dao.insert(newsBean);
            platformTransactionManager.commit(status);
        } catch (Exception e) {
            e.printStackTrace();
            platformTransactionManager.rollback(status);
        }
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void setTransactionManager(PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public void setNewsBean(NewsBean newsBean) {
        this.newsBean = newsBean;
    }

    public void setPlatformTransactionManager(my.spring.transaction.PlatformTransactionManager platformTransactionManager) {
        this.platformTransactionManager = platformTransactionManager;
    }
}
